package cuiying.rbac.service.array;

import cuiying.rbac.domain.dto.DeptDto;
import cuiying.rbac.domain.dto.UserDto;
import cuiying.rbac.domain.entity.Dept;
import cuiying.rbac.domain.entity.Role;
import cuiying.rbac.domain.entity.User;
import cuiying.rbac.domain.vo.DeptVo;
import cuiying.rbac.domain.vo.Result;
import cuiying.rbac.domain.vo.UserVo;
import cuiying.rbac.service.IDeptService;
import cuiying.rbac.service.IRoleService;
import cuiying.rbac.service.IUserService;

import java.util.Arrays;

/**
 * 业务层-用户
 *
 * @author 崔颖
 * @since 2025/5/18 13:56
 */
public abstract class UserService extends BaseService<User, UserDto,UserVo>
        implements IUserService {


    public static int deptId;
    public static int roleId;

    private final IDeptService deptService;
    private final IRoleService roleService;

    private static IUserService singleton;

    private UserService(){
        deptService = DeptService.getInstance();
        roleService = RoleService.getInstance();
        data = new User[DATA_SIZE];
        temp = new UserVo[ DATA_SIZE];

    }
    public static IUserService getInstance(){
        if(singleton == null){
            singleton = new UserService();
        }
        return singleton;
    }


    //ctrl+z 撤回前一步
    @Override
    UserVo converToVo(User user) {
        String deptName = "";
        Dept dept = deptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }
        String roleName = "";
        Role role = roleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }
        return new UserVo(user.getId(), user.username, user.name, user.mobile,
                user.email, deptId, roleId,roleName,user.enabled);
    }
    @Override
    User creatEntity(UserDto dto, int id){
        return new User(id,dto.username ,  dto.name , dto.mobile ,
                dto.email , dto.deptId , dto.roleId);
    }
    @Override
    void updataEntity(UserDto dto, User user){
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    public boolean deptInUse(int deptId){
        boolean inUse = false;
        for(User user : data){
            if(user != null && user.deptId == deptId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public boolean roleInUse(int roleId){
        boolean inUse = false;
        for(User user : data){
            if(user != null && user.roleId == roleId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public Result create(UserDto dto){
        if(!deptService.existsById(dto.deptId)){
            return Result.failure("创建失败，不存在指定的部门");
        }
        if(!roleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在指定的角色");
        }
        return super.create(dto);

    }

    public Result update(UserDto dto, int id){

        if(!deptService.existsById(dto.deptId)){
            return Result.failure("创建失败，不存在指定的部门");
        }
        if(!roleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在指定的角色");
        }
        return super.update(dto, id);
    }

    @Override
    public Result toggle(int id) {
        if (!existsById(id)) {
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enabled = !data[id].enabled;
        return Result.success(data[id].enabled ? "启用成功" : "禁用成功", null);
    }

    public Result updata(UserDto dto, int id) {
        return null;
    }
}
